﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Status;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Status;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 资产状况
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/status")]
    public class StatusController : ApiController
    {
        /// <summary>
        /// 状况列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("infos")]
        public async Task<IHttpActionResult> GetStatusList()
        {
            return this.SendData(await StatusManger.GetList());
        }

        /// <summary>
        /// 添加状况
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("infos")]
        public async Task<IHttpActionResult> CreateStatus(CreateStatusViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            return await StatusManger.Create(model.StatusName) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 获取状况信息
        /// </summary>
        /// <param name="statusid">状况Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{statusid}/infos")]
        public async Task<IHttpActionResult> GetStatus(Guid statusid) {
           return this.SendData(await StatusManger.GetById(statusid));
        }

        /// <summary>
        /// 更新状况名称
        /// </summary>
        /// <param name="statusid">状况Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{statusid}/infos")]
        public async Task<IHttpActionResult> UpdateStatus(Guid statusid, StatusModel model)
        {
            model.Id = statusid;
            return await StatusManger.Update(model) ? this.SendData("修改成功") : this.ErrorData("修改失败");
        }

        /// <summary>
        /// 删除状况
        /// </summary>
        /// <param name="statusid">状况Id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{statusid}/infos")]        
        public async Task<IHttpActionResult> DeleteStatus(Guid statusid) {
            return await StatusManger.Delete(statusid) ? this.SendData("删除成功") : this.ErrorData("删除失败");
        }

        /// <summary>
        /// 获取资产状况统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("countinfo")]
        public async Task<IHttpActionResult> GetCountInfo()
        {
            var data = await StatusManger.GetCountInfo();
            return this.SendData(data);
        }

    }
}
